#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
     ui->comboBoxtargetip->addItem(GetLocalIp());
     udpsocket=new QUdpSocket(this);
     connect(udpsocket,&QUdpSocket::readyRead,this,&MainWindow::SocketReadyReadData);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_start_clicked()
{
    quint16 port=ui->spinBoxbindport->value();//绑定本地端口

    if(udpsocket->bind(port))//socket绑定指定端口
    {
        ui->plainTextEditdispmsg->appendPlainText("**********绑定成功**********");
        ui->plainTextEditdispmsg->appendPlainText("$$$$$$$$$$绑定端口$$$$$$$$$$："+
                                                  QString::number(udpsocket->localPort()));
        ui->pushButton_start->setEnabled(false);
        ui->pushButton_stop->setEnabled(true);

    }
    else
    {
        ui->plainTextEditdispmsg->appendPlainText("**********绑定失败**********");
    }

}

void MainWindow::on_pushButton_stop_clicked()
{
   udpsocket->abort();
   ui->pushButton_stop->setEnabled(false);
   ui->pushButton_start->setEnabled(true);
}

void MainWindow::on_pushButton_sendmsg_clicked()
{
    QString msg=ui->lineEditmsg->text();
    ui->lineEditmsg->clear();
    QString targetip=ui->comboBoxtargetip->currentText();
    quint16 targetport=ui->spinBoxtargetport->value();//获取目标端口
    QHostAddress ip(targetip);
    QByteArray str=msg.toUtf8();
    udpsocket->writeDatagram(str,ip,targetport);//发送数据
    ui->plainTextEditdispmsg->appendPlainText("[out]："+str);
    ui->lineEditmsg->setFocus(); // 将光标焦点定位到编辑框控件

}

void MainWindow::on_pushButton_broadcastmsg_clicked()
{
       quint16 targetport=ui->spinBoxtargetport->value();
       QString strmsg=ui->lineEditmsg->text();
       QByteArray str=strmsg.toUtf8();
       udpsocket->writeDatagram(str,QHostAddress::Broadcast,targetport);

       ui->plainTextEditdispmsg->appendPlainText("[broadcast]："+str);
       ui->lineEditmsg->clear(); // 清除编辑框控件内容
       ui->lineEditmsg->setFocus(); // 将光标焦点定位到编辑框控件
}

QString MainWindow::GetLocalIp()
{
    QString hostname=QHostInfo::localHostName();
    QHostInfo hostinfo=QHostInfo::fromName(hostname);
    QList<QHostAddress> address=hostinfo.addresses();
    QString ip="";
    if(!address.isEmpty())
    {
    for(int i=0;i<address.count();i++)
    {
       QHostAddress temp=address.at(i);
       if(temp.protocol()==QAbstractSocket::IPv4Protocol)
       {
           ip=temp.toString();
           break;
       }
    }
    }
    return ip;
}

void MainWindow::SocketReadyReadData()
{
   while(udpsocket->hasPendingDatagrams())//判断是否有内容可以读取
      {
       QByteArray datagrams;
       datagrams.resize(udpsocket->pendingDatagramSize());
       QHostAddress paddress;//发送方的ip
       quint16 padport;//发送方的port
       udpsocket->readDatagram(datagrams.data(),datagrams.size(),&paddress,&padport);
       QString str=datagrams.data();
       QString peer="[From:"+paddress.toString()+":"+QString::number(padport)+"]:";

       ui->plainTextEditdispmsg->appendPlainText(peer+str);

   }


}

